<!--
 * @Descripttion: 订单管理
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2022-01-25 11:45:32
-->
<template>
  <div class="lb-order-manage">
    <top-nav />
    <div class="page-main">
      <el-row class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="场地名称" prop="place_title">
            <el-input
              v-model="searchForm.place_title"
              placeholder="请输入场地名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="客户手机号" prop="phone">
            <el-input
              v-model="searchForm.phone"
              placeholder="请输入客户手机号"
            ></el-input>
          </el-form-item>
          <el-form-item label="系统订单号" prop="order_code">
            <el-input
              v-model="searchForm.order_code"
              placeholder="请输入系统订单号"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="pay_type">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.pay_type"
              placeholder="请选择"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="预订日期" prop="start_time">
            <el-date-picker
              @change="getTableDataList(1)"
              v-model="range"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="timestamp"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="所属公司" prop="company_id">
            <el-select
              v-model="searchForm.company_id"
              filterable
              placeholder="请选择所属公司"
            >
              <el-option
                v-for="item in base_company"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-row class="page-top-operate">
        <lb-button
          size="mini"
          plain
          type="primary"
          icon="el-icon-printer"
          @click="printTable"
          >打印</lb-button
        >
        <lb-button
          size="mini"
          plain
          type="primary"
          icon="el-icon-download"
          :loading="downloadLoading"
          @click="exportOrder"
          >导出</lb-button
        >
        <!-- <el-dropdown>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>导出到Csv文件</el-dropdown-item>
                <el-dropdown-item>导出到Excel文件</el-dropdown-item>
              </el-dropdown-menu>
        </el-dropdown>-->
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="ID"
          width="80"
          sortable
          fixed
        ></el-table-column>
        <el-table-column prop="" width="300" label="场地信息">
          <template slot-scope="scope">
            <div class="table-goods-info">
              <lb-image :src="scope.row.place_cover" />
              <div class="goods-info-r">
                <p class="f-paragraph c-title">{{ scope.row.place_title }}</p>
                <p>
                  {{ scope.row.table_text }}
                </p>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="user_id" label="客户ID"></el-table-column>
        <el-table-column
          prop="user_info.nickName"
          label="客户昵称"
        ></el-table-column
        ><el-table-column
          prop="phone"
          label="客户手机号"
          min-width="120"
        ></el-table-column>
        <el-table-column prop="staff_name" label="员工"></el-table-column>
        <el-table-column label="公司/部门" min-width="200">
          <template slot-scope="scope">
            <div>
              {{ scope.row.company_name }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="place_price" label="预订日期" min-width="200">
          <template slot-scope="scope">
            {{ scope.row.time_date | handleTime(1) }}
            {{ `(${scope.row.week}) ${scope.row.time_type}` }}
          </template>
        </el-table-column>
        <el-table-column prop="place_price" label="预付金">
          <template slot-scope="scope">¥{{ scope.row.place_price }} </template>
        </el-table-column>
        <el-table-column prop="phone" label="支付方式">
          <template slot-scope="scope">
            <div>
              {{ scope.row.balance * 1 > 0 ? '储值支付' : '微信支付' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="order_code" width="150" label="系统订单号">
        </el-table-column>
        <el-table-column prop="transaction_id" width="150" label="商户订单号">
        </el-table-column>
        <el-table-column prop="create_time" width="120" label="下单时间">
          <template slot-scope="scope">
            <div>{{ scope.row.create_time | handleTime(1) }}</div>
            <div>{{ scope.row.create_time | handleTime(2) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ payType[scope.row.pay_type] }}</template
          ></el-table-column
        >
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                type="primary"
                plain
                @click="
                  $router.push(
                    `${appPath}/banquet/order/detail?id=${scope.row.id}`
                  )
                "
                >{{ $t('action.view') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 打印表格 -->
      <table
        v-show="print"
        style="font-size: 12px"
        ref="print"
        border="0"
        cellspacing="0"
        cellapdding="0"
        class="print-table"
      >
        <thead>
          <tr>
            <th
              style="border: 1px solid"
              v-for="(item, index) in printTableData[0]"
              :key="index"
            >
              {{ item }}
            </th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in printTableData[1]" :key="index">
            <td style="border: 1px solid" v-for="(items, i) in item" :key="i">
              {{ items }}
            </td>
          </tr>
        </tbody>
      </table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
import pubic from '@/utils/public'
// import reg from '@/utils/reg'
export default {
  data () {
    return {
      appPath: '',
      loading: false,
      downloadLoading: false,
      print: false,
      payType: {
        '-1': '已取消',
        '1': '待付款',
        '2': '已完成'
      },
      searchForm: {
        page: 1,
        limit: 10,
        place_title: '',
        company_id: '',
        phone: '',
        order_code: '',
        transaction_id: '',
        pay_type: 0
      },
      base_company: [],
      range: '',
      statusOptions: [
        {
          label: '全部订单',
          value: 0
        },
        {
          label: '已取消',
          value: '-1'
        },
        {
          label: '待付款',
          value: 1
        },
        {
          label: '已完成',
          value: 2
        },
        {
          label: '已评价',
          value: 7
        }
      ],
      tableData: [],
      printTableData: [],
      total: 0,
      multipleSelection: []
    }
  },
  activated () {
    let { path } = this.$route
    this.appPath = path.includes('app/') ? '/app' : ''
    this.getTableDataList()
    this.getSelectOptions()
  },
  methods: {
    resetForm (name) {
      this.range = ''
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    /**
     * @method 搜索订单列表
     */
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      let { range } = this
      if (range && range.length > 0) {
        searchForm.start_time = range[0] / 1000
        searchForm.end_time = range[1] / 1000 + 24 * 3600 - 1
      } else {
        searchForm.start_time = ''
        searchForm.end_time = ''
      }
      if (searchForm.pay_type === 7) {
        searchForm.have_eva = 1
        delete searchForm.pay_type
      }
      this.$api.banquet.orderList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
          let arr = res.data.data.map(item => {
            let table = item.table_text ? ` 容纳桌数：${item.table_text} ` : ''
            item.goods_text = item.place_title + table
            return [
              item.id,
              item.goods_text,
              item.user_id,
              item.user_info.nickName,
              item.phone,
              item.staff_name,
              item.company_name,
              moment(item.time_date * 1000).format('YYYY-MM-DD') + ` (${item.week}) ${item.time_type}`,
              '¥' + item.place_price,
              item.balance * 1 > 0 ? '储值支付' : '微信支付',
              item.order_code,
              item.transaction_id,
              moment(item.create_time * 1000).format('YYYY-MM-DD HH:mm:ss'),
              this.payType[item.pay_type]
            ]
          })
          this.printTableData = [
            [
              'ID',
              '场地信息',
              '客户ID',
              '客户昵称',
              '客户手机号',
              '员工',
              '公司/部门',
              '预订日期',
              '预付金',
              '支付方式',
              '系统订单号',
              '商户订单号',
              '下单时间',
              '状态'
            ],
            arr
          ]
        }
      })
    },
    /**
     * @method 导出订单
     */
    exportOrder () {
      this.downloadLoading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      let range = JSON.parse(JSON.stringify(this.range))
      if (range && range.length > 0) {
        searchForm.start_time = range[0] / 1000
        searchForm.end_time = range[1] / 1000 + 24 * 3600 - 1
      } else {
        searchForm.start_time = ''
        searchForm.end_time = ''
      }
      if (searchForm.pay_type === 7) {
        searchForm.have_eva = 1
        delete searchForm.pay_type
      }
      let url = pubic.getProCurrentHref()
      let keywords = url.indexOf('?') > 0 ? '' : '?'
      let flag = url.indexOf('?') > 0
      Object.getOwnPropertyNames(searchForm).forEach((keys, value) => {
        keywords += flag
          ? `&${keys}=${searchForm[keys]}`
          : `${keys}=${searchForm[keys]}`
        flag = true
      })
      let token = sessionStorage.getItem('minitk')
      let dwonlaodUrl = `${url}/banquet/AdminExcel/orderExcel${keywords}&token=${token}`
      window.location.href = dwonlaodUrl
      setTimeout(() => {
        this.downloadLoading = false
      }, 5000)
    },
    /**
     * @method 获取下拉菜单的选项值
     */
    getSelectOptions () {
      this.$api.getBaseCompany().then(res => {
        if (res.code === 200) {
          this.base_company = res.data
        }
      })
    },
    /**
     * @method 打印表格
     */
    printTable () {
      this.print = true
      setTimeout(() => {
        this.$print(this.$refs.print)
        this.print = false
      }, 50)
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-order-manage {
  .page-main {
    width: 100%;
    padding: 20px;

    .el-select,
    .el-input-number,
    .el-input {
      width: 200px;
    }
    .el-table {
      .table-goods-info {
        width: 280px;
        height: 80px;
        display: flex;
        align-items: center;
        margin-bottom: 10px;
        font-size: 12px;

        .goods-info-r {
          width: 100%;
          display: flex;
          flex-direction: column;
          justify-content: space-around;

          p {
            width: 210px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }

          .price {
            color: red;
          }
        }

        .el-image {
          min-width: 70px;
          height: 70px;
          margin-right: 10px;
        }

        &:last-child {
          margin-bottom: 0;
        }
      }
    }
    .print-table {
      position: fixed;
      bottom: -999999px;
    }
  }
}

.none {
  display: none;
}
</style>
<style media="print">
.none {
  display: block;
}
</style>
